package com.heima.Server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.Mapper.UserMapper;
import com.heima.model.Login.LoginDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import com.heima.utils.common.SaltUtil;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class UserServer implements com.heima.Server.UserServer {
    @Autowired
    UserMapper userMapper;

    //登录
    @Override
    public ResponseResult Login(LoginDto loginDto) {
        //游客登录
        if(loginDto==null)
        {
            String jwt=AppJwtUtil.getToken(0L);
            Map<String ,Object> map=new HashMap<>();
            map.put("token",jwt);
            return ResponseResult.okResult(map);
        }

        //查询用户是否存在
        QueryWrapper<ApUser> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",loginDto.getPhone());
        ApUser apUser=userMapper.selectOne(wrapper);
        //存在
        if(apUser!=null)
        {
            //确认密码是否正确
            String passwd=MD5Utils.encodeWithSalt(loginDto.getPassword(),apUser.getSalt());
            //密码错误
            if(!passwd.equals(apUser.getPassword()))
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,
                        "密码错误");

            String jwt=AppJwtUtil.getToken(apUser.getId().longValue());
            Map<String ,Object> map=new HashMap<>();
            map.put("token",jwt);
            //为了避免信息泄露，要把不需要的数据清空
            apUser.setSalt("");
            apUser.setPassword("");
            map.put("user",apUser);
            return ResponseResult.okResult(map);
        }
        //不存在
        else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,
                    "用户不存在");
        }
    }

//    添加用户
    @Override
    public ResponseResult Useradd(ApUser apUser) {
        if(IfExist(apUser.getId())==false)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,
                    "用户已存在");
        if(apUser.getPassword().isEmpty())
            apUser.setPassword("123456");
        //生成盐字符串
        apUser.setSalt(SaltUtil.GetSalt());
//        用户默认值设置
        apUser.setCreatedTime(LocalDateTime.now().toDate());
        apUser.setCertification(false);
        apUser.setIdentityAuthentication(false);

        if(apUser.getFlag()==null)
        apUser.setFlag((short) 0);

        apUser.setStatus(true);
        //给密码进行MD5加密
        apUser.setPassword(MD5Utils.encodeWithSalt(apUser.getPassword(),apUser.getSalt()));
        Integer num=userMapper.insert(apUser);
        if(num==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,
                    "用户添加失败");

        return ResponseResult.okResult(num);
    }

    //删除用户
    @Override
    public ResponseResult USerDelete(Integer id) {
        if(IfExist(id)==true)
            return  ResponseResult.okResult(200,"用户已删除或者不存在");
        userMapper.deleteById(id);
        return  ResponseResult.okResult(200,"用户已删除");
    }

    //用户详情
    @Override
    public ResponseResult Userinfo(Integer id) {
       ApUser apUser= userMapper.selectById(id);
       if(apUser==null)
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,
                "没有此用户");

        return ResponseResult.okResult(apUser);
    }

    //判断用户是否存在
    private boolean IfExist(Integer id)
    {
        QueryWrapper<ApUser> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        //用户已存在
        if(userMapper.selectOne(wrapper)!=null)
            return false;

        return true;
    }



}
